<template>
  <div class>
    <el-card class="btns">
      <el-button size="medium" type="primary" @click="submitForm('ruleForm')">查询</el-button>
      <el-button size="medium" @click="resetForm('ruleForm')">重置</el-button>
      <el-button size="medium" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button>
    </el-card>
    <el-card style="position:relative;">
      <el-form :style="style" :model="ruleForm" ref="ruleForm" inline label-width="100px" class="demo-ruleForm">
        <div class="icon-arrow" v-if="folding">
          <i @click="offForm=!offForm" :title="offForm?'展开表单':'收起表单'"
            :class="offForm?'el-icon-arrow-down':'el-icon-arrow-up'"></i>
        </div>
        <el-form-item label="职务名称" prop="dutyName">
          <el-input v-model="ruleForm.dutyName"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="dutyEnabledState">
          <el-select v-model="ruleForm.dutyEnabledState" clearable placeholder="请选择">
            <el-option v-for="item in dutyEnabledState" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <el-table stripe :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
      <el-table-column align="center" label="职务名称">
        <template slot-scope="scope">
          <span>{{scope.row.dutyName}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="职务类型">
                        <template slot-scope="scope">
                                  <span>{{scope.row.dutyType}}</span>
                        </template>
      </el-table-column>-->
      <el-table-column align="center" label="职务介绍">
        <template slot-scope="scope">
          <span>{{scope.row.dutyIntroduce}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span>{{scope.row.dutyEnabledState}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注">
        <template slot-scope="scope">
          <span>{{scope.row.remark}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="success" @click="configUser(scope.$index, scope.row)">配置</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next"
        :total="total"></el-pagination>
    </el-card>
    <!-- --------------------------------------------------------添加弹出框 -->
    <el-dialog title="新增管理" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="form" ref="addForm" :rules="rules">
        <el-form-item label="职务名称" :label-width="formLabelWidth" prop="dutyName">
          <el-input v-model="form.dutyName" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="职务类型" :label-width="formLabelWidth" prop="dutyType">
                          <el-input v-model="form.dutyType" autocomplete="off"></el-input>
        </el-form-item>-->
        <el-form-item label="职务机构" :label-width="formLabelWidth" prop="premLevel">
          <zzjg ref="zzjg" @change="getPremLevel" parentDialog></zzjg>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="dutyEnabledState">
          <el-switch v-model="switchValue" active-text="启用" inactive-text="禁用" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="职务介绍" :label-width="formLabelWidth" prop="dutyIntroduce">
          <el-input v-model="form.dutyIntroduce" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------编辑弹出框 -->
    <el-dialog title="编辑管理" :visible.sync="dialogEditVisible" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="职务名称" :label-width="formLabelWidth" prop="dutyName">
          <el-input v-model="editForm.dutyName" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="职务类型" :label-width="formLabelWidth" prop="dutyType">
                        <el-input v-model="editForm.dutyType" autocomplete="off"></el-input>
        </el-form-item>-->
        <el-form-item label="职务机构" :label-width="formLabelWidth" prop="premLevel">
          <zzjg @change="getEditPremLevel" :initV="initZzjg" ref="editZzjg" parentDialog></zzjg>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="dutyEnabledState">
          <el-switch v-model="switchEditValue" active-text="启用" inactive-text="禁用" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="职务介绍" :label-width="formLabelWidth" prop="dutyIntroduce">
          <el-input v-model="editForm.dutyIntroduce" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="editForm.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import zzjg from "@/components/zzjg";
  export default {
    components: { zzjg },
    data() {
      return {
        dutyEnabledState: [
          {
            value: "1",
            label: "启用"
          },
          {
            value: "0",
            label: "禁用"
          }
        ],
        users: [],
        initZzjg: [],
        switchValue: "1",
        switchEditValue: "1",
        sysOrganList: [],
        dialogFormVisible: false,
        dialogEditVisible: false,
        offForm: true, //是否折叠
        folding: false, //表单是否超过一行
        formLabelWidth: "80px",
        tableData: [],
        total: 0,
        pageSize: 10,
        currentPage: 1,
        ruleForm: {
          dutyCode: "",
          dutyName: "",
          dutyType: "",
          dutyIntroduce: "",
          dutyEnabledState: "",
          remark: ""
        },
        style: {},
        rules: {
          dutyName: [
            { required: true, message: "请输入职务名称", trigger: "blur" }
          ]
        },
        form: {
          dutyCode: "",
          dutyName: "",
          dutyType: "",
          dutyIntroduce: "",
          dutyEnabledState: "",
          remark: ""
        },
        editForm: {
          id: "",
          //  dutyCode:'',
          dutyName: "",
          dutyType: "",
          dutyIntroduce: "",
          dutyEnabledState: "",
          remark: ""
        }
      };
    },

    mounted() {
      this.init();
      this.getusers();
      let formHeight = this.$refs.ruleForm.$el.clientHeight;
      if (formHeight > 62) {
        this.folding = true;
        this.style = {
          height: "62px",
          overflow: "hidden"
        };
      } else {
        this.folding = false;
        this.style = {
          height: "auto",
          overflow: "hidden"
        };
      }
    },
    computed: {},
    watch: {
      offForm: function (v) {
        if (v) {
          this.style = {
            height: "62px",
            overflow: "hidden"
          };
        } else {
          this.style = {
            height: "auto",
            overflow: "hidden"
          };
        }
      }
    },
    methods: {
      getPremLevel(val) {
        this.sysOrganList = val;
      },
      getEditPremLevel(val) {
        this.sysOrganList = val;
      },
      sizeChange(n) {
        this.pageSize = n;
        this.init();
      },
      currentChange(n) {
        this.currentPage = n;
        this.init();
      },
      init() {
        let params = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.ruleForm
        };
        this.$api.formPost("/api/auth/sysDuty/doGetList", params).then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
      },
      showDialog() {
        this.dialogFormVisible = true;
        this.form = {};
        this.$nextTick(() => {
          this.$refs.zzjg.resetChecked();
        });
      },
      configUser(index, row) {
        //跳路由
        this.$router.push({
          path: "/auth/configUser",
          query: { dutyId: row.id }
        });
      },
      showEditDialog(index, row) {
        console.log(row)
        this.initZzjg = []
        let list = row.sysDutyOrganList;
        for (let key in list) {
          this.initZzjg.push(list[key].organId);
        }
        this.editForm.id = row.id;
        this.editForm.dutyName = row.dutyName;
        this.switchEditValue = row.dutyEnabledState == "启用" ? "1" : "0";
        this.editForm.dutyType = row.dutyType;
        this.editForm.dutyIntroduce = row.dutyIntroduce;
        this.editForm.remark = row.remark;
        this.$nextTick(() => {
          this.$refs.editZzjg.resetChecked();
        });
        this.dialogEditVisible = true;
      },
      handleAdd() {
        this.form.sysOrganList = this.sysOrganList;
        this.form.dutyEnabledState = this.switchValue;
        this.$refs.addForm.validate(valid => {
          if (valid) {
            this.$api.post("/api/auth/sysDuty/doSave", this.form).then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "添加成功！",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.init();
              } else {
                this.$message({
                  message: res.retHead.message,
                  type: "error"
                });
              }
            });
          }
        });
      },
      handleEdit() {
        this.$refs.editForm.validate(valid => {
          if (valid) {
            this.editForm.sysOrganList = this.sysOrganList;
            this.editForm.dutyEnabledState = this.switchEditValue;
            let params = this.editForm;
            this.$api.post("/api/auth/sysDuty/doModById", params).then(res => {
              if (res.retHead.code == "0000000") {
                this.$message.success("执行成功！");
                this.init();
                this.dialogEditVisible = false;
              } else {
                this.$message.error(res.retHead.message);
              }
            });
          }
        });
      },
      getusers() {
        let params = {};
        this.$api.formPost("/api/auth/sysuser/doGetList", params).then(res => {
          if (res.retHead.code == "0000000") {
            this.users = res.retBody.list;
          }
        });
      },
      handleDelete(index, row) {
        this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.$api
              .formPost("/api/auth/sysDuty/doRmById", {
                id: row.id
              })
              .then(res => {
                if (res.retHead.code == "0000000") {
                  this.$message({
                    message: "删除成功！",
                    type: "success"
                  });
                  this.init();
                } else {
                  this.$message({
                    message: "删除失败！",
                    type: "error"
                  });
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      submitForm(formName) {
        this.init();
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() { }
  };
</script>
<style lang='scss' scoped>
  .btns {
    text-align: right;
  }

  .icon-arrow {
    position: absolute;
    bottom: 5px;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #f1f1f1;

    i {
      background-color: #fff;
      position: absolute;
      color: #999;
      bottom: -7px;
      margin-left: 50%;
      transform: translateX(-50%);
      cursor: pointer;
    }
  }
</style>